/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.easoft.code.pos.ui.beans;

import com.easoft.code.pos.util.Valores;
import java.lang.reflect.Method;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

/**
 *
 * @author Eliu
 */
public class FormFieldDatePic extends javax.swing.JPanel implements DataEntryField, ReportParamInput {

    private String label;
    private Object value;
    private String toolTip;
    private String sourceName;
    private Object targetObject;
    private Method methodListening;

    @Override
    public Object getTargetObject() {
        return targetObject;
    }

    @Override
    public void setTargetObject(Object targetObject) {
        this.targetObject = targetObject;
    }

    @Override
    public Method getMethodListening() {
        return methodListening;
    }

    @Override
    public void setMethodListening(Method methodListening) {
        this.methodListening = methodListening;
    }

    @Override
    public String getSourceName() {
        return sourceName;
    }

    @Override
    public void setSourceName(String sourceName) {
        this.sourceName = sourceName;
    }

    @Override
    public String getToolTip() {
        return toolTip;
    }

    @Override
    public void setToolTip(String toolTip) {
        this.toolTip = toolTip;
    }

    @Override
    public String getLabel() {
        return label;
    }

    @Override
    public void setLabel(String label) {
        this.fieldLabel.setText(label);
        this.label = label;
    }

    @Override
    public Object getValue() {
        this.value = this.txtDateInput.getDate();
        return value;
    }

    @Override
    public void setValue(Object value) {
        this.value = value;
    }

    public FormFieldDatePic(String label, Object value, String sourceName) {
        this.label = label;
        this.value = value;
        this.sourceName = sourceName;
        initComponents();
        this.fieldLabel.setText(label);
        this.txtDateInput.setToolTipText(label);
    }

    /**
     * Creates new form FormField
     */
    public FormFieldDatePic() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        fieldLabel = new javax.swing.JLabel();
        txtDateInput = new com.toedter.calendar.JDateChooser();

        setLayout(new java.awt.GridLayout(1, 0, 0, 5));

        fieldLabel.setText("Nombre Largo");
        fieldLabel.setPreferredSize(new java.awt.Dimension(150, 16));
        add(fieldLabel);
        add(txtDateInput);
    }// </editor-fold>//GEN-END:initComponents

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel fieldLabel;
    private com.toedter.calendar.JDateChooser txtDateInput;
    // End of variables declaration//GEN-END:variables

    @Override
    public void ready() {
        try {
            this.fieldLabel.setText(label);
            SimpleDateFormat sd = new SimpleDateFormat(Valores.getConfigValue(Valores.DATE_FORMAT));
            this.txtDateInput.setDate(value != null ? sd.parse(value.toString()) : Calendar.getInstance().getTime());
        } catch (ParseException ex) {
            this.txtDateInput.setDate(Calendar.getInstance().getTime());
        }
    }

    @Override
    public String getInputKeyName() {
        return this.sourceName;
    }

    @Override
    public Object getCurrentValue() {
        return this.txtDateInput.getDate();
    }

    @Override
    public void cleanValue() {
        this.txtDateInput.cleanup();
    }
}
